import Vue from "vue";
import VueRouter from "vue-router";

//import Login from "../components/Login";
//import Index from "../components/Index";
//import Welcome from "../components/Welcome";
//const Login =()=> import("components/Login")
const Login =()=> import("../components/Login")
const Index =()=> import("../components/Index")
const Welcome =()=> import("../components/Welcome")
const User =()=> import("../components/user/User")
const Role =()=> import("../components/role/Roles")
const Rights =()=> import("../components/role/Rights")
const Goods =()=> import("../components/goods/Goods")
const Add =()=> import("../components/goods/Add")
const Category =()=> import("../components/goods/Category")
const Params =()=> import("../components/goods/Params")
const Order =()=> import("../components/order/Order")
const Report=()=> import("../components/report/Report")
//1.使用 Vue.use(VueRouter) 安装插件
Vue.use(VueRouter)

//2.创建路由对象
const routes = [
    {
        path: '',
        redirect:'/index'
    },
    {
        path:'/login',
        component:Login,
        meta:{
            title:'登录',
        }
    },
    {
        path: '/index',
        component: Index,
        redirect:'/welcome',
        children:[
            {path:'/welcome',component:Welcome,
                meta: {
                    title: 'welcome'
                }
            },
            {path:'/users',component:User,
                meta: {
                    title: '用户列表'
                }
            },{
            path:'/roles',component:Role,
                meta: {
                    title: '角色列表'
                }
            },
            {
                path:'/rights',component:Rights,
                meta: {
                    title: '权限列表'
                }
            },
            {
                path:'/goods',component:Goods,
                meta: {
                    title: '商品列表'
                }
            },
            {
                path:'/goods/add',component:Add,
                meta: {
                    title: '添加商品'
                }
            },
            {
                path:'/params',component:Params,
                meta: {
                    title: '分类参数'
                }
            },
            {
                path:'/categories',component:Category,
                meta: {
                    title: '商品分类'
                }
            },
            {
                path:'/orders',component:Order,
                meta: {
                    title: '订单列表'
                }
            },
            {
                path:'/reports',component:Report,
                meta: {
                    title: '数据报表'
                }
            }
        ],
        meta: {
            title: '首页'
        },
    }
]
//3.配置路由与组件的关系
const router = new VueRouter({
    routes,
    mode:'history'
})
// 设置 title
// 前置钩子函数
router.beforeEach((to, from, next)=>{
    // 获取 token
    const token = window.sessionStorage.getItem("token");
    document.title = to.meta.title
    //document.title = to.matched[0].meta.title
    if (to.path == '/login') return next();

    // 没有 token 强制 跳转 登录页面
    if (!token) return next("/login")
    next()

})
//4. 将router 对象 传入 vue 实例
export default router


